<?php

namespace app\admin\controller;

use app\common\model\CardLevel;
use app\common\model\Delivery;
use app\common\model\Order as OrderModel;
use app\common\model\OrderList;
use app\common\model\OrderListRecovery;
use app\common\model\OrderListSend;
use app\common\model\Shang;
use app\common\model\User;
use think\facade\View;
use think\Request;
use think\facade\Db;


class Order extends Base
{

    /**
     * 卡单列表
     */
    public function kadan_order()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $order_num = trim(input('get.order_num'));
        $addtime = trim(input('get.addtime'));

        $whe = array();
        $whe[] = ['status', '=', 0];
        $whe[] = ['kd_is', '=', 1];
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($order_num) {
            $whe[] = ['order_num', '=', $order_num];
        }
        if ($addtime) {
            $time = explode(' - ', $addtime);
            $start_time = strtotime($time[0]);
            $end_time = strtotime($time[1]) - 1;
            if ($start_time > $end_time) {
                $this->err('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
        }
        $field = '*';
        $order = 'id desc';
        $data = OrderModel::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $user_info = User::field('nickname,headimg,mobile')->where(['id' => $value['user_id']])->find();
            $value['nickname'] = $user_info['nickname'];
            $value['headimg'] = $user_info['headimg'];
            $value['mobile'] = $user_info['mobile'];
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch('Order/kadan_order');
    }

    /**
     * 购买列表
     */
    public function buy_order()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $order_num = trim(input('get.order_num'));
        $addtime = trim(input('get.addtime'));

        $whe = array();
        $whe[] = ['status', '=', 1];
        $whe[] = ['kd_is', '=', 0];
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($order_num) {
            $whe[] = ['order_num', '=', $order_num];
        }
        if ($addtime) {
            $time = explode(' - ', $addtime);
            $start_time = strtotime($time[0]);
            $end_time = strtotime($time[1]) - 1;
            if ($start_time > $end_time) {
                $this->err('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
            $str_addtime = $start_time . '_' . $end_time;
        } else {
            $str_addtime = 0;
        }
        $field = '*';
        $order = 'id desc';
        $data = OrderModel::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $user_info = User::field('nickname,headimg,mobile')->where(['id' => $value['user_id']])->find();
            $value['nickname'] = $user_info['nickname'];
            $value['headimg'] = $user_info['headimg'];
            $value['mobile'] = $user_info['mobile'];
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('user_id', $user_id ? $user_id : 0);
        View::assign('mobile', $mobile ? $mobile : 0);
        View::assign('order_num', $order_num ? $order_num : 0);
        View::assign('str_addtime', $str_addtime ? $str_addtime : 0);
        return View::fetch('Order/buy_order');
    }

    /**
     * 购买奖品详情
     */
    public function buy_order_detail()
    {
        $id = request()->param('id/d', 0);
        $info = OrderModel::field('id')->where(['id' => $id])->find();
        if (!$info) {
            return $this->renderError('请求参数错误');
        }
        
        #数据
        $data = OrderList::field('goodslist_title,goodslist_imgurl,goodslist_price,goodslist_money,shang_id,count(`id`) as num,order_type')
//            ->append(['shang_title'])
            ->where('order_id', '=', $id)
            ->group('prize_code')
            ->order('id desc')
            ->select()->toArray();
        foreach ($data as &$value){

            if($value['order_type'] == 4){
                $shang_title = CardLevel::field('title')->where('id',$value['shang_id'])->value('title');
            }else{
                $shang_title = Shang::field('title')->where(['id' => $value['shang_id']])->value('title');
            }
            if (!$shang_title){
                $shang_title = '无';
            }
            $value['shang_title'] = $shang_title;
        }
//        dd($data);
        View::assign('list', $data);
        View::assign('count', count($data));
        return View::fetch('Order/buy_order_detail');
    }

    /**
     * 兑换列表
     */
    public function recovery_order()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $addtime = trim(input('get.addtime'));

        $whe = array();
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($addtime) {
            $time = explode(' - ', $addtime);
            $start_time = strtotime($time[0]);
            $end_time = strtotime($time[1]) - 1;
            if ($start_time > $end_time) {
                $this->err('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
            $str_addtime = $start_time . '_' . $end_time;
        } else {
            $str_addtime = 0;
        }
        $field = '*';
        $order = 'id desc';
        $data = OrderListRecovery::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $user_info = User::field('nickname,headimg,mobile')->where(['id' => $value['user_id']])->find();
            $value['nickname'] = $user_info['nickname'];
            $value['headimg'] = $user_info['headimg'];
            $value['mobile'] = $user_info['mobile'];
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('user_id', $user_id ? $user_id : 0);
        View::assign('mobile', $mobile ? $mobile : 0);
        View::assign('str_addtime', $str_addtime ? $str_addtime : 0);
        return View::fetch('Order/recovery_order');
    }

    /**
     * 兑换奖品详情
     */
    public function recovery_order_detail()
    {
        $id = request()->param('id/d', 0);
        $info = OrderListRecovery::field('recovery_num,id')->where(['id' => $id])->find();
        if (!$info) {
            return $this->renderError('请求参数错误');
        }
        #数据
        $data = OrderList::field('goodslist_title,goodslist_imgurl,goodslist_price,goodslist_money,shang_id,count(`id`) as num')
            ->append(['shang_title'])
            ->where('recovery_num', '=', $info['recovery_num'])
            ->group('prize_code')
            ->order('id desc')
            ->select()->toArray();
        View::assign('list', $data);
        View::assign('count', count($data));
        return View::fetch('Order/recovery_order_detail');
    }


    /**
     * 订单列表
     */
    public function send_order()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $send_num = trim(input('get.send_num'));
        $status = trim(input('get.status'));
        $addtime = trim(input('get.addtime'));

        $whe = array();
        if ($status) {
            $whe[] = ['status', '=', $status];
        } else {
            $whe[] = ['status', '>', 0];
        }
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($send_num) {
            $whe[] = ['send_num', '=', $send_num];
        }
        if ($addtime) {
            $time = explode(' - ', $addtime);
            $start_time = strtotime($time[0]);
            $end_time = strtotime($time[1]) - 1;
            if ($start_time > $end_time) {
                $this->err('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
            $str_addtime = $start_time . '_' . $end_time;
        } else {
            $str_addtime = 0;
        }
        $field = '*';
        $order = 'id desc';
        $data = OrderListSend::getList($whe, $field, $order, $this->page);
        foreach ($data['list'] as &$value) {
            $user_info = User::field('nickname,headimg,mobile')->where(['id' => $value['user_id']])->find();
            $value['nickname'] = $user_info['nickname'];
            $value['headimg'] = $user_info['headimg'];
            $value['mobile'] = $user_info['mobile'];
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('user_id', $user_id ? $user_id : 0);
        View::assign('mobile', $mobile ? $mobile : 0);
        View::assign('send_num', $send_num ? $send_num : 0);
        View::assign('status', $status ? $status : 0);
        View::assign('str_addtime', $str_addtime ? $str_addtime : 0);
        return View::fetch('Order/send_order');
    }

    /**
     * 订单详情
     */
    public function send_order_detail()
    {
        $id = request()->param("id", '');
        $info = OrderListSend::where('id', '=', $id)->find();
        if (!$info) {
            return $this->renderError('请求参数错误');
        }
        if ($info['status'] != 1 && $info['status'] != 2 && $info['status'] != 3) {
            return $this->renderError('请求参数错误1');
        }
        #数据
        $data = OrderList::field('goodslist_title,goodslist_imgurl,goodslist_price,goodslist_money,shang_id,count(`id`) as num')
            ->append(['shang_title'])
            ->where('send_num', '=', $info['send_num'])
            ->group('prize_code')
            ->order('id desc')
            ->select()->toArray();
            
        foreach ($data as $k => &$v){
            $v['goodslist_imgurl'] = imageUrl($v['goodslist_imgurl']);
        }    

        View::assign('info', $info);
        View::assign('list', $data);
        View::assign('count', count($data));
        return View::fetch("Order/send_order_detail");
    }


    /**
     * 发货
     */
    public function send_order_deliver(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param("id", '');
            $info = OrderListSend::where('id', '=', $id)->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            $delivery = Delivery::select();
            View::assign("id", $id);
            View::assign("delivery", $delivery);
            return View::fetch('Order/send_order_deliver');
        } else {
            $id = $request->param("id", '');
            $courier_name = $request->param("courier_name", '');
            $courier_number = $request->param("courier_number", '');
            $info = OrderListSend::where('id', '=', $id)->find();
            if (!$info) {
                return $this->renderError('请求参数错误');
            }
            // if ($info['status'] == 2) {
            //     return $this->renderError("请勿重复操作");
            // }
            // if ($info['status'] !== 1) {
            //     return $this->renderError("订单状态异常");
            // }
            if (empty($courier_name)) {
                return $this->renderError("请选择物流公司");
            }
            if (empty($courier_number)) {
                return $this->renderError("请输入物流单号");
            }
            $delivery_info = Delivery::where(['id' => $courier_name])->find();
            if (!$delivery_info) {
                return $this->renderError("物流公司选择错误");
            }
            $res = OrderListSend::where(array("id" => $id))->update([
                'status' => 2,
                'courier_name' => $delivery_info['name'],
                'courier_code' => $delivery_info['code'],
                'courier_number' => $courier_number,
                'send_time' => time(),
            ]);
            if ($res) {
                return $this->renderSuccess("发货成功，请等待客户签收");
            } else {
                return $this->renderError("发货失败");

            }
        }
    }


    /**
     * 发货订单取消
     */
    public function cancel_order()
    {
        $id = request()->param('id/d', 0);
        $info = OrderListSend::where('id', '=', $id)->find();
        if (!$info) {
            return $this->renderError('请求参数错误');
        }
        if ($info['status'] == 4) {
            return $this->renderError('请勿重复操作');
        }
        if ($info['status'] != 1) {
            return $this->renderError('已发货不能退');
        }
        if ($info['freight'] > 0) {
            $wxServer = new \app\common\server\WechatRefund($this->app);
            $refund_res = $wxServer->OrderRefund($info);
            Db::name('wxpay_log')->insert([
                'order_no' => 'QX_' . $info['send_num'],
                'content' => json_encode($refund_res['msg']),
                'type' => 2,
            ]);
//            if ($refund_res['status'] == 0) {
//                return $this->renderError($refund_res['msg']);
//            }
        }
        Db::startTrans();
        $res = [];
        #改变发货订单状态
        $res[] = OrderListSend::field('status,cancel_time,admin_id')
            ->where(['id' => $info['id']])->update([
                'status' => 4,
                'cancel_time' => time(),
                'admin_id' => $this->admin_id,
            ]);
        #改变赏品信息
        $res[] = OrderList::field('send_num,status,choice_time')
            ->where('status', '=', 2)
            ->where('send_num', '=', $info['send_num'])
            ->update([
                'send_num' => NULL,
                'status' => 0,
                'choice_time' => 0,
            ]);
        if (resCheck($res)) {
            Db::commit();
            return $this->renderSuccess('取消成功');
        } else {
            Db::rollback();
            return $this->renderError('取消失败');
        }
    }

    /**
     * 购买订单导出
     */
    public function buy_order_daochu()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $order_num = trim(input('get.order_num'));
        $addtime = trim(input('get.addtime'));
        $whe = array();
        $whe[] = ['status', '=', 1];
        $whe[] = ['kd_is', '=', 0];
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($order_num) {
            $whe[] = ['order_num', '=', $order_num];
        }
        if ($addtime) {
            $time = explode('_', $addtime);
            $start_time = $time[0];
            $end_time = $time[1];
            if ($start_time > $end_time) {
                return $this->renderError('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
        }
        #数据
        $data = OrderModel::where($whe)
            ->append(['user_info'])
            ->order('id desc')
            ->select()
            ->toArray();
        if (!$data) {
            return $this->renderError('未找到数据');
        }
        $save_name = substr(dirname($_SERVER['SCRIPT_FILENAME']), 0, -6);
        require $save_name . 'extend/phpexcel/PHPExcel.php';
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '订单号')
            ->setCellValue('B1', '用户信息')
            ->setCellValue('C1', '盒子信息')
            ->setCellValue('D1', '盒子类型')
            ->setCellValue('E1', '订单金额')
            ->setCellValue('F1', '折扣')
            ->setCellValue('G1', '折扣金额')
            ->setCellValue('H1', '微信支付')
            ->setCellValue('I1', '余额抵扣')
            ->setCellValue('J1', '潮币抵扣')
            ->setCellValue('K1', '积分支付')
            ->setCellValue('L1', '时间');
        //设置A列水平居中
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(25);

        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('L')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:L1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:L1')->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setBold(true);

        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(40);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(50);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(50);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(30);

        $line = 1;
        foreach ($data as $key => $value) {
            $line++;
            #订单号
            $order_num = $value['order_num'];
            #用户信息
            $user_info = 'ID：' . $value['user_id'] . "     " . '昵称：' . $value['user_info']['nickname'] . "     " . '手机号：' . $value['user_info']['mobile'];
            #奖品信息
            $goods_info = '盒子名称：' . $value['goods_title'];
            if ($value['num']) {
                $goods_info .= "     " . '箱号：' . $value['num'];
            }
            #盒子类型
            $order_type_name = '';
            if ($value['order_type'] == 1) {
                $order_type_name = '一番赏';
            } elseif ($value['order_type'] == 2) {
                $order_type_name = '无限赏';
            } elseif ($value['order_type'] == 3) {
                $order_type_name = '擂台赏';
            } elseif ($value['order_type'] == 4) {
                $order_type_name = '抽卡机';
            } elseif ($value['order_type'] == 5) {
                $order_type_name = '积分赏';
            }
            #时间
            $addtime = date('Y-m-d H:i:s', $value['addtime']);
            #开始导出
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $line, $order_num);#订单号
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $line, $user_info);#用户信息
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $line, $goods_info);#盒子信息
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $line, $order_type_name);#盒子类型
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $line, $value['order_total']);#订单金额
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $line, $value['zhe']);#折扣
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $line, $value['order_zhe_total']);#折扣金额
            $objPHPExcel->getActiveSheet()->setCellValue('H' . $line, $value['price']);#微信支付
            $objPHPExcel->getActiveSheet()->setCellValue('I' . $line, $value['use_money']);#余额抵扣
            $objPHPExcel->getActiveSheet()->setCellValue('J' . $line, $value['use_integral']);#潮币抵扣
            $objPHPExcel->getActiveSheet()->setCellValue('K' . $line, $value['use_score']);#积分支付
            $objPHPExcel->getActiveSheet()->setCellValue('L' . $line, $addtime);#时间

        }
        //7.设置保存的Excel表格名称
        $filename = '购买列表信息' . date('y-m-d H_i_s', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('现金套餐订单信息');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }


    /**
     * 兑换订单导出
     */
    public function recovery_order_daochu()
    {

        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $addtime = trim(input('get.addtime'));
        $whe = array();
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($addtime) {
            $time = explode('_', $addtime);
            $start_time = $time[0];
            $end_time = $time[1];
            if ($start_time > $end_time) {
                return $this->renderError('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
        }
        #数据
        $data = OrderListRecovery::where($whe)
            ->append(['user_info', 'status_name'])
            ->order('id desc')
            ->select()
            ->toArray();
        if (!$data) {
            return $this->renderError('未找到数据');
        }
        $save_name = substr(dirname($_SERVER['SCRIPT_FILENAME']), 0, -6);
        require $save_name . 'extend/phpexcel/PHPExcel.php';
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '用户信息')
            ->setCellValue('B1', '兑换金额')
            ->setCellValue('C1', '兑换数量')
            ->setCellValue('D1', '兑换时间');
        //设置A列水平居中
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(25);

        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:D1')->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);

        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(70);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);

        $line = 1;
        foreach ($data as $key => $value) {
            $line++;
            #用户信息
            $user_info = 'ID：' . $value['user_id'] . "     " . '昵称：' . $value['user_info']['nickname'] . "     " . '手机号：' . $value['user_info']['mobile'];
            #申请时间
            $addtime = date('Y-m-d H:i:s', $value['addtime']);
            #开始导出
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $line, $user_info);#用户信息
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $line, $value['money']);#兑换金额
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $line, $value['count']);#兑换数量
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $line, $addtime);#兑换时间
        }
        //7.设置保存的Excel表格名称
        $filename = '兑换列表信息' . date('y-m-d H_i_s', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('现金套餐订单信息');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;
    }


    /**
     * 发货订单导出
     */
    public function send_order_daochu()
    {
        $user_id = trim(input('get.user_id'));
        $mobile = trim(input('get.mobile'));
        $send_num = trim(input('get.send_num'));
        $status = trim(input('get.status'));
        $addtime = trim(input('get.addtime'));
        if ($status == 4) {
            return $this->renderError('不可导出已取消状态');
        }
        $whe = array();
        if ($status) {

            $whe[] = ['status', '=', $status];
        } else {
            $whe[] = ['status', 'in', [1, 2, 3]];
        }
        if ($user_id) {
            $whe[] = ['user_id', '=', $user_id];
        }
        if ($mobile) {
            $mobile = User::field('id')->where('mobile', '=', $mobile)->value('id');
            $whe[] = ['user_id', '=', $mobile];
        }
        if ($send_num) {
            $whe[] = ['send_num', '=', $send_num];
        }
        if ($addtime) {
            $time = explode('_', $addtime);
            $start_time = $time[0];
            $end_time = $time[1];
            if ($start_time > $end_time) {
                return $this->renderError('开始时间不能大于结束时间');
            }
            $whe[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
        }
        #数据
        $data = OrderListSend::where($whe)
            ->append(['user_info', 'status_name'])
            ->order('id desc')
            ->select()
            ->toArray();
        if (!$data) {
            return $this->renderError('未找到数据');
        }

        $save_name = substr(dirname($_SERVER['SCRIPT_FILENAME']), 0, -6);
        require $save_name . 'extend/phpexcel/PHPExcel.php';
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '订单号')
            ->setCellValue('B1', '用户信息')
            ->setCellValue('C1', '状态')
            ->setCellValue('D1', '收货姓名')
            ->setCellValue('E1', '收货电话')
            ->setCellValue('F1', '收货地址')
            ->setCellValue('G1', '奖品名称')
            ->setCellValue('H1', '奖品价格')
            ->setCellValue('I1', '奖品数量')
            ->setCellValue('J1', '赠送编码')
            ->setCellValue('K1', '奖品类型')
            ->setCellValue('L1', '申请时间')
            ->setCellValue('M1', '备注');
        //设置A列水平居中
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(25);

        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('L')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('M')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:M1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:M1')->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:M1')->getFont()->setBold(true);

        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(40);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(50);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(50);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(30);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(30);

        $line = 1;
        foreach ($data as $key => $value) {
            #订单号
            $send_num = $value['send_num'];
            #用户信息
            $user_info = 'ID：' . $value['user_id'] . "     " . '昵称：' . $value['user_info']['nickname'] . "     " . '手机号：' . $value['user_info']['mobile'];
            #状态
            $status = $value['status_name'];
            #收货姓名
            $name = $value['name'];
            #收货电话
            $mobile = $value['mobile'];
            #收货地址
            $address = $value['address'];
            #申请时间
            $addtime = date('Y-m-d H:i:s', $value['addtime']);
            #奖品信息
            $goods = OrderList::field('goodslist_id,goodslist_title,goodslist_price,goodslist_money,shang_id,count(`id`) as num')
                ->append(['shang_title', 'card_no'])
                ->where('send_num', '=', $value['send_num'])
                ->group('prize_code')
                ->order('id desc')
                ->select()->toArray();
            #备注
            $message = $value['message'];
            foreach ($goods as $goods_key => $goods_value) {
                $line++;
                #赠送编码
                $card_no = $goods_value['card_no'];
                #奖品信息
                $goods_title = $goods_value['goodslist_title'];
                $goods_price = $goods_value['goodslist_price'];
                #奖品数量
                $goods_num = $goods_value['num'];
                #奖品类型
                $goods_shang_title = $goods_value['shang_title'];
                #开始导出
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $line, $send_num);#订单号
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $line, $user_info);#用户信息
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $line, $status);#状态
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $line, $name);#收货姓名
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $line, $mobile);#收货电话
                $objPHPExcel->getActiveSheet()->setCellValue('F' . $line, $address);#收货详细地址
                $objPHPExcel->getActiveSheet()->setCellValue('G' . $line, $goods_title);#奖品名称
                $objPHPExcel->getActiveSheet()->setCellValue('H' . $line, $goods_price);#奖品价格
                $objPHPExcel->getActiveSheet()->setCellValue('I' . $line, $goods_num);#奖品数量
                $objPHPExcel->getActiveSheet()->setCellValue('J' . $line, $card_no);#赠送编码
                $objPHPExcel->getActiveSheet()->setCellValue('K' . $line, $goods_shang_title);#奖品类型
                $objPHPExcel->getActiveSheet()->setCellValue('L' . $line, $addtime);#申请时间
                $objPHPExcel->getActiveSheet()->setCellValue('M' . $line, $message);#备注
            }
        }
        //7.设置保存的Excel表格名称
        $filename = '发货列表信息' . date('y-m-d H_i_s', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('现金套餐订单信息');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }

}